package com.example.clicker;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.NinePatchDrawable;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.EditText;

public class TimecodeDialog extends Activity implements OnClickListener,OnKeyListener{
	
	private static final int REQUEST_SAVE = 0;
	private static final int REQUEST_LOAD = 1;
	Button btn,iabtn,inbtn,ipbtn,bs,bc;
    EditText et;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dialogs);
		//View mDialog = getLayoutInflater().inflate(R.layout.dialogs, null); // Получаем layout по его ID
		btn = (Button) findViewById(R.id.CustomButton);
        btn.setOnClickListener(this);
        
        bs = (Button) findViewById(R.id.bSave);
        bs.setOnClickListener(this);
        
        bc = (Button) findViewById(R.id.bCancel);
        bc.setOnClickListener(this);
        
        et = (EditText) findViewById(R.id.EditTextButton);
        et.setOnKeyListener(this);
   
        iabtn= (Button) findViewById(R.id.ButtonImageActive);
        inbtn= (Button) findViewById(R.id.ButtonImageNormal);
        ipbtn= (Button) findViewById(R.id.ButtonImagePressed);
		
        iabtn.setOnClickListener(this);
        inbtn.setOnClickListener(this);
        ipbtn.setOnClickListener(this);
		
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Intent intent = new Intent(getBaseContext(), FileDialog.class);
		switch(v.getId())
		{
		case R.id.ButtonImageActive:
			//can user select directories or not
            intent.putExtra(FileDialog.CAN_SELECT_DIR, true);
           	intent.putExtra(FileDialog.FORMAT_FILTER, new String[] { "png" });
           	intent.putExtra("ImageButton", R.id.ButtonImageActive);
			startActivityForResult(intent, REQUEST_LOAD);
		break;
		case R.id.ButtonImageNormal:
			//can user select directories or not
            intent.putExtra(FileDialog.CAN_SELECT_DIR, true);
           	intent.putExtra(FileDialog.FORMAT_FILTER, new String[] { "png" });
         	intent.putExtra("ImageButton", R.id.ButtonImageNormal);
			startActivityForResult(intent, REQUEST_LOAD);
		break;
	
		case R.id.ButtonImagePressed:
			//can user select directories or not
            intent.putExtra(FileDialog.CAN_SELECT_DIR, true);
           	intent.putExtra(FileDialog.FORMAT_FILTER, new String[] { "png" });
           	intent.putExtra("ImageButton", R.id.ButtonImagePressed);
        	startActivityForResult(intent, REQUEST_LOAD);
		break;
	
		case R.id.bSave:
			finish();
			
		break;
		case R.id.bCancel:
			finish();
		
		default:
		}
		
	}

	
	@Override
	public boolean onKey(View v, int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
				switch(v.getId())
				{
				case R.id.EditTextButton:
					//can user select directories or not
		 				btn.setText(((EditText)et).getText().toString());
			       
				break;
				case R.id.ButtonImageNormal:
			 			btn.setText(((EditText)et).getText().toString());
		 		
					
				break;
				case R.id.ButtonImagePressed:
		 				btn.setText(((EditText)et).getText().toString());
		 		
				default:
				}
		return false;
	}

	 public synchronized void onActivityResult(final int requestCode,
             int resultCode, final Intent data) {

             if (resultCode == Activity.RESULT_OK) 
             {

                     if (requestCode == REQUEST_LOAD) {
                            
                     
                     String filePath = data.getStringExtra(FileDialog.RESULT_PATH);
                     int nbi=data.getIntExtra("ImageButton",0);
                     if(nbi==R.id.ButtonImageNormal)
                     {
                    	 ((EditText)findViewById(R.id.EditTextImageNormal)).setText(filePath);
                    	 if(filePath.length()!=0)
                    	 {
                    		Drawable dw=btn.getResources().getDrawable(android.R.drawable.btn_default);
                    		NinePatchDrawable npdw=(NinePatchDrawable) btn.getResources().getDrawable(android.R.drawable.btn_default);
                     	 }
                     }
                     if(nbi==R.id.ButtonImagePressed)
                     {
                    	 ((EditText)findViewById(R.id.EditTextImagePressed)).setText(filePath);
                     }
                     if(nbi==R.id.ButtonImageActive)
                     {
                    	 ((EditText)findViewById(R.id.EditTextImageActive)).setText(filePath);
                     }								} 
             }
	 }
}